package com.maszy.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.maszy.bean.StaffLogin;
import com.maszy.commons.tools.MD5;
import com.maszy.service.StaffLoginService;

public class StaffLoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	StaffLoginService service = new StaffLoginService();
	String view = new String(); // 页面路径

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		StaffLoginServlet servlet = new StaffLoginServlet();
		RequestDispatcher rd = null;
		String method = request.getParameter("method");
		if ("queryAll".equals(method)) {
			// 查询出全部数据
			servlet.queryAll(request, response);
			view = "/background/stafflogin/stafflogin.jsp";
		} else if ("insert".equals(method)) {
			// 添加新记录
			servlet.add(request, response);
			servlet.queryAll(request, response);
			view = "/background/stafflogin/stafflogin.jsp";
		} else if ("update".equals(method)) {
			// 修改记录
			servlet.updatePassword(request, response);
			servlet.queryAll(request, response);
			view = "/background/stafflogin/stafflogin.jsp";
		} else if ("delete".equals(method)) {
			// 删除记录
			servlet.delete(request, response);
			servlet.queryAll(request, response);
			view = "/background/stafflogin/stafflogin.jsp";
		} else if ("updateForward".equals(method)) {
			// 进入修改页面
			servlet.queryForOne(request, response);
			view = "/background/stafflogin/update_stafflogin.jsp";
		} else if ("insertForward".equals(method)) {
			// 进入添加页面
			servlet.insertForward(request, response);
			view = "/background/stafflogin/add_stafflogin.jsp";
		}else if ("login".equals(method)) {
			// 登陆
			int i=servlet.Login(request, response);
			if(i==0){
				request.setAttribute("error", "success");
				servlet.updateLogin(request, response);
				view="/web/admin/error.jsp";
			}else if(i==9){
				request.setAttribute("error", "store");
				servlet.updateLogin(request, response);
				view="/web/admin/error.jsp";
			}
			else{
				view="/web/admin/error.jsp";
			}
		}else if("quit".equals(method)){
			request.getSession().removeAttribute("m");
			request.setAttribute("error", "quit");
			view="/web/admin/error.jsp";
		}

		try {
			rd = this.getServletContext().getRequestDispatcher(view);
			rd.forward(request, response);

		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void updateLogin(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		StaffLogin obj=(StaffLogin) request.getSession().getAttribute("m");	
		obj.setStaffLogin_IP(request.getRemoteAddr());
		String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()); 
		obj.setStaffLogin_LastTime(time);
		service.update(obj, "login");
	}

	private void insertForward(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String temp = request.getParameter("Staff_Name");
		try {
			temp = new String(temp.getBytes("iso-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("Staff_Name", temp);
		request.setAttribute("StaffLogin_Id", request
				.getParameter("StaffLogin_Id"));
	}

	private void queryForOne(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("StaffLogin_Id");
		if (!"".equals(id) && id != null) {
			StaffLogin obj = new StaffLogin();
			obj.setStaffLogin_Id(Long.parseLong(id));
			obj = service.queryById(obj);
			request.setAttribute("obj", obj);
		}
		String temp = request.getParameter("Staff_Name");
		try {
			temp = new String(temp.getBytes("iso-8859-1"), "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("Staff_Name", temp);
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id = request.getParameter("StaffLogin_Id");
		if (!"".equals(id) && id != null) {
			StaffLogin obj = new StaffLogin();
			obj.setStaffLogin_Id(Long.parseLong(id));
			service.delete(obj);
		}
	}

	private void updatePassword(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		StaffLogin obj = new StaffLogin();
		String id = request.getParameter("StaffLogin_Id");
		if (!"".equals(id) && id != null) {
			
			obj.setStaffLogin_Remark(request.getParameter("StaffLogin_Remark"));
//			service.update(obj, "update");
		}
	}

	private void queryAll(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		List list = service.queryAll();
		request.setAttribute("list", list);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub	
		String id = request.getParameter("StaffLogin_Id");
		if (!"".equals(id) && id != null) {
			StaffLogin obj = new StaffLogin();
			obj.setStaffLogin_Id(Long.parseLong(id));
			obj.setStaffLogin_LoginName(request.getParameter("StaffLogin_LoginName"));		
			obj.setStaffLogin_LoginPassword(MD5.encodePassword(request.getParameter("StaffLogin_LoginPassword"), "MD5"));
			obj.setLog_NoteFlag(Integer.parseInt(request.getParameter("Log_NoteFlag")));
			obj.setStaffLogin_Remark(request.getParameter("StaffLogin_Remark"));

			service.add(obj);
		}
	}
	private int Login(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		StaffLogin obj = new StaffLogin();
		int i=1;
		String StaffLogin_LoginName=request.getParameter("StaffLogin_LoginName");
		String StaffLogin_LoginPassword=request.getParameter("StaffLogin_LoginPassword");		
		String rand = (String) request.getSession().getAttribute("rand");
		
		if(rand.equals(request.getParameter("checknum"))){
			if("".equals(StaffLogin_LoginName)){
				request.setAttribute("error", "用户名不能为空");
			}
			else if("".equals(StaffLogin_LoginPassword)){				
				request.setAttribute("error", "登陆密码不能为空");	
				return i;
			}else{
				StaffLogin_LoginPassword=MD5.encodePassword(StaffLogin_LoginPassword,"MD5");
				obj=StaffLoginService.adminLogin(StaffLogin_LoginName,StaffLogin_LoginPassword);	
				
				if(obj.getStaffLogin_Id()>0){
					request.getSession().setAttribute("m",obj);
					i=0;
					if(obj.getDoorStore_Id()!=1){
						i=9;
					}
				}else{
					request.setAttribute("error", "对不起,您输入的用户名或密码有误,请重新登陆！");
				}	
			}		
		}else{
			request.setAttribute("error", "验证码输入错误");
		}
		return i;
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		doGet(request, response);
	}
}